package ysq.xyg.shop.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import ysq.xyg.base.model.dto.ReturnEntity;
import ysq.xyg.base.service.impl.BaseServiceImpl;
import ysq.xyg.base.util.BaseConstant;
import ysq.xyg.shop.dao.ShopItemDao;
import ysq.xyg.shop.model.po.ShopItem;
import ysq.xyg.shop.model.vo.ShopItemVo;
import ysq.xyg.shop.service.ShopItemService;
import ysq.xyg.sys.model.po.SysUser;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 商城模块-商品service实现
 *
 * @author congcong tao
 * @date 2019-4-28 11:18:52
 */
@Service("shopItemService")
public class ShopItemServiceImpl extends BaseServiceImpl<ShopItem> implements ShopItemService {

    @Resource
    private ShopItemDao shopItemDao;

    private ShopItem shopItem;

    @Autowired
    public void setBaseDao(ShopItemDao shopItemDao) {
        this.baseDao = shopItemDao;
    }

    /**
     * 分页查询
     *
     * @param shopItemVo 对象
     * @param pageSize 展示数量
     * @param pageNum 当前页面
     * @return ReturnEntity 对象
     * @author congcong tao
     * @date 2019-4-28 11:18:52
     */
    public ReturnEntity page(ShopItemVo shopItemVo, int pageSize, int pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        PageHelper.orderBy(" update_time desc");
        List<ShopItem> list = acceptList(shopItemVo);
        PageInfo<ShopItem> page=new PageInfo<ShopItem>(list);
        return new ReturnEntity(page.getList(),page.getTotal(),page.isHasNextPage());
    }

    /**
     * 修改
     *
     * @param shopItemVo 对象
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author congcong tao
     * @date 2019-4-28 11:18:52
     */
    @Override
    public ReturnEntity addOrUpdate(ShopItemVo shopItemVo, SysUser currentUser) {
        boolean isUpate=true;
        if(StrUtil.isEmpty(shopItemVo.getId())){
            shopItem=new ShopItem(currentUser);
            isUpate=false;
        }else{
            shopItem=selectByPrimaryKey(shopItemVo.getId());
            if(ObjectUtil.isNull(shopItem)){
                return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
            }
            shopItem.setUpdateTime(new Date());
            shopItem.setUpdateUser(currentUser.getId());
        }
        shopItem.setBrandId(shopItemVo.getBrandId());
        shopItem.setCatId(shopItemVo.getCatId());
        shopItem.setTitle(shopItemVo.getTitle());
        shopItem.setImage(shopItemVo.getImage());
        shopItem.setContent(shopItemVo.getContent());
        shopItem.setBank(shopItemVo.getBank());
        shopItem.setDescription(shopItemVo.getDescription());
        shopItem.setPrePrice(shopItemVo.getPrePrice());
        shopItem.setOrigPrice(shopItemVo.getOrigPrice());
        shopItem.setKeyWord(shopItemVo.getKeyWord());
        shopItem.setType(shopItemVo.getType());
        shopItem.setRemainderNum(shopItemVo.getRemainderNum());
        shopItem.setOrderNum(shopItemVo.getOrderNum());
        shopItem.setStatus(shopItemVo.getStatus());
        int num=0;
        if(isUpate){
            num=updateByPrimaryKey(shopItem);
        }else{
            num=insert(shopItem);
        }
        if(num<=0){
            return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
        }
        return new ReturnEntity();
    }
    /**
     * 删除
     *
     * @param id 主键id
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author congcong tao
     * @date 2019-4-28 11:18:52
     */
    @Override
    public ReturnEntity delete(String id, SysUser currentUser) {
        ShopItem shopItem=selectByPrimaryKey(id);
        if(shopItem!=null){
            shopItem.setRemoveTag(BaseConstant.BASE_REMOVE_TAG_DELETE);
            shopItem.setUpdateTime(new Date());
            shopItem.setUpdateUser(currentUser.getId());
            int num=updateByPrimaryKey(shopItem);
            if(num>0){
                return new ReturnEntity();
            }
        }
        return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
    }

    /**
     * 获取集合
     *
     * @param shopItemVo 对象
     * @return List<BaseDict> BaseDict集合
     * @author congcong tao
     * @date 2019-4-28 11:18:52
     */
    @Override
    public List<ShopItem> acceptList(ShopItemVo shopItemVo){
        Example example = new Example(ShopItem.class);
        Example.Criteria criteria = example.createCriteria();
        if(StringUtil.isNotEmpty(shopItemVo.getTitle())){
            criteria.andLike("title","%" + shopItemVo.getTitle() + "%");
        }
        if(StringUtil.isNotEmpty(shopItemVo.getBank())){
            criteria.andEqualTo("bank", shopItemVo.getBank());
        }
        if(StringUtil.isNotEmpty(shopItemVo.getCatId())){
            criteria.andEqualTo("catId", shopItemVo.getCatId());
        }
        if(StringUtil.isNotEmpty(shopItemVo.getBrandId())){
            criteria.andEqualTo("brandId", shopItemVo.getBrandId());
        }
        if(StringUtil.isNotEmpty(shopItemVo.getRemoveTag())){
            criteria.andEqualTo("removeTag", shopItemVo.getRemoveTag());
        }
        example.setOrderByClause(" update_time desc");
        return shopItemDao.selectByExample(example);
    }

    @Override
    public List<ShopItem> selectShopItemList(ShopItemVo shopItemVo) {
        return shopItemDao.selectShopItemList(shopItemVo);
    }
}
